package org.arch_learn.global_session.spring_redis_session.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;

@Controller
@RequestMapping(value = "/login")
public class LoginController {

    @RequestMapping(value = "/to-login", method = RequestMethod.GET)
    public String toLogin() {
        System.out.println("跳转登录页面================");
        return "login";
    }

    @RequestMapping(value = "/do-login", method = RequestMethod.POST)
    public String doLogin(@NotBlank(message = "用户名不能为空！") String username,
                          @NotBlank(message = "密码不能为空！") String password,
                          HttpSession httpSession) {
        String redirect = null;
        if ("admin".equals(username) && "admin".equals(password)) {
            System.out.println("登录成功！");
            httpSession.setAttribute("username", username + System.currentTimeMillis());
            redirect = "redirect:/welcome";
        } else {
            System.out.println("密码不正确！");
            redirect = "redirect:/login/to-login";
        }
        return redirect;
    }
}
